/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.penguin.modules.field.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.penguin.modules.field.entity.FieldDailyTask;
import com.thinkgem.penguin.modules.field.service.FieldDailyTaskService;

/**
 * 日常任务生成Controller
 * @author 余小凡
 * @version 2015-05-22
 */
@Controller
@RequestMapping(value = "${adminPath}/field/fieldDailyTask")
public class FieldDailyTaskController extends BaseController {

	@Autowired
	private FieldDailyTaskService fieldDailyTaskService;
	
	@ModelAttribute
	public FieldDailyTask get(@RequestParam(required=false) String id) {
		FieldDailyTask entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = fieldDailyTaskService.get(id);
		}
		if (entity == null){
			entity = new FieldDailyTask();
		}
		return entity;
	}
	@RequiresPermissions("field:fieldDailyTask:view")
	@RequestMapping(value = {"finals", ""})
	public String finals(FieldDailyTask fieldDailyTask, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<FieldDailyTask> page = fieldDailyTaskService.findPage(new Page<FieldDailyTask>(request, response), fieldDailyTask); 
		model.addAttribute("page", page);
		return "modules/field/fieldDailyTaskList_finals";
	}
	
	@RequiresPermissions("field:fieldDailyTask:view")
	@RequestMapping(value = {"task", ""})
	public String task(FieldDailyTask fieldDailyTask, HttpServletRequest request, HttpServletResponse response, Model model) {
	 
		return "modules/field/fieldDailyTaskForm_task";
	}
	
	
	@RequiresPermissions("field:fieldDailyTask:view")
	@RequestMapping(value = {"audit", ""})
	public String audit(FieldDailyTask fieldDailyTask, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<FieldDailyTask> page = fieldDailyTaskService.findPage(new Page<FieldDailyTask>(request, response), fieldDailyTask); 
		model.addAttribute("page", page);
		return "modules/field/fieldDailyTaskList_audit";
	}	
	@RequiresPermissions("field:fieldDailyTask:view")
	@RequestMapping(value = {"list", ""})
	public String list(FieldDailyTask fieldDailyTask, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<FieldDailyTask> page = fieldDailyTaskService.findPage(new Page<FieldDailyTask>(request, response), fieldDailyTask); 
		model.addAttribute("page", page);
		return "modules/field/fieldDailyTaskList";
	}

	@RequiresPermissions("field:fieldDailyTask:view")
	@RequestMapping(value = "form")
	public String form(FieldDailyTask fieldDailyTask, Model model) {
		model.addAttribute("fieldDailyTask", fieldDailyTask);
		return "modules/field/fieldDailyTaskForm";
	}

	@RequiresPermissions("field:fieldDailyTask:edit")
	@RequestMapping(value = "save")
	public String save(FieldDailyTask fieldDailyTask, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, fieldDailyTask)){
			return form(fieldDailyTask, model);
		}
		fieldDailyTaskService.save(fieldDailyTask);
		addMessage(redirectAttributes, "保存日常任务生成成功");
		return "redirect:"+Global.getAdminPath()+"/field/fieldDailyTask/list?repage";
	}
	
	@RequiresPermissions("field:fieldDailyTask:edit")
	@RequestMapping(value = "delete")
	public String delete(FieldDailyTask fieldDailyTask, RedirectAttributes redirectAttributes) {
		fieldDailyTaskService.delete(fieldDailyTask);
		addMessage(redirectAttributes, "删除日常任务生成成功");
		return "redirect:"+Global.getAdminPath()+"/field/fieldDailyTask/list?repage";
	}

}